package com.zx.idc.backend.gui.api.commom.factory.digest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by hasee on 2019/2/20.
 */
public class SHA1DigestImpl implements Digest {

    @Override
    public String digest(String targetString, String password, boolean isBase64Encode) {
        String encodeStr = DigestUtils.sha1Hex(targetString + password);
        return isBase64Encode ? base64Encode(encodeStr) : encodeStr;
    }

    @Override
    public String digest(byte[] targetBytes, String password, boolean isBase64Encode) {
        if (targetBytes != null && StringUtils.isNotEmpty(password))
            targetBytes = ArrayUtils.addAll(targetBytes, password.getBytes());
        String encodeStr = DigestUtils.sha1Hex(targetBytes);
        return isBase64Encode ? base64Encode(encodeStr) : encodeStr;
    }

    @Override
    public String digestWithoutPassword(byte[] targetBytes, boolean isBase64Encode) {
        String encodeStr = DigestUtils.sha1Hex(targetBytes);
        return isBase64Encode ? base64Encode(encodeStr) : encodeStr;
    }

    @Override
    public String digestWithoutPassword(String targetString, boolean isBase64Encode) {
        String encodeStr = DigestUtils.sha1Hex(targetString);
        return isBase64Encode ? base64Encode(encodeStr) : encodeStr;
    }

}
